﻿using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;
using Skyworth.CRM.Service.Entity;
using Skyworth.CRM.Service.IDAL;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Skyworth.CRM.DAL
{
    public class RoleDAL :SuperDataBase, IRoleDAL
    {
       //public RoleDAL()
       //    :base()
       //{

       //}
       //public RoleDAL(string dbName)
       //    : base(dbName)
       //{

       //}

        public void Add(Roles model)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into T_SYS_Roles(");
                strSql.Append("RoleName,RoleDescription,IsDelete,CreateTime,LastModifyTime)");
                strSql.Append(" values (");
                strSql.Append("@RoleName,@RoleDescription,0,getdate(),getdate())");
                //Database db = DatabaseFactory.CreateDatabase();               
                DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
                DB.AddInParameter(cmd, "@RoleName", DbType.String, model.RoleName);
                DB.AddInParameter(cmd, "@RoleDescription", DbType.String, model.RoleDescription);
                DB.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                throw ex;
            }            
        }

        public void Update(Roles model)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update T_SYS_Roles set ");
                strSql.Append("RoleName=@RoleName,");
                strSql.Append("RoleDescription=@RoleDescription,");
                strSql.Append("IsDelete=@IsDelete,");
                strSql.Append("LastModifyTime=getdate()");
                strSql.Append(" where RoleID=@RoleID");
               // Database db = DatabaseFactory.CreateDatabase();
                DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
                DB.AddInParameter(cmd, "@RoleID", DbType.Int32, model.RoleId);
                DB.AddInParameter(cmd, "@RoleName", DbType.String, model.RoleName);
                DB.AddInParameter(cmd, "@RoleDescription", DbType.String, model.RoleDescription);
                DB.AddInParameter(cmd, "@IsDelete", DbType.Boolean, model.IsDelete);
                DB.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                throw ex;
            }          
        }

        public DataTable Query(string strWhere)
        {
            try
            {
                string sql = "SELECT * FROM [T_SYS_Roles] where [IsDelete] = 0";
                sql += strWhere+" order by RoleName asc";;
               // Database db = DatabaseFactory.CreateDatabase();
                DataSet ds = DB.ExecuteDataSet(CommandType.Text, sql);
                return ds.Tables[0];
            }
            catch (Exception ex)
            {                
                throw ex;
            }            
        }
        public DataTable Query(string strWhere, Hashtable parameterValue)
        {
            try
            {
                string strCommandText = "SELECT * FROM [T_SYS_Roles] where [IsDelete] = 0";
                strCommandText += strWhere+" order by RoleName asc";
                System.Data.Common.DbCommand sqlcommand = DB.GetSqlStringCommand(strCommandText);
                if (parameterValue != null && parameterValue.Count != 0)
                {
                    foreach (DictionaryEntry de in parameterValue)
                    {
                        DB.AddInParameter(sqlcommand, de.Key.ToString(), DbType.String);
                        DB.SetParameterValue(sqlcommand, de.Key.ToString(), de.Value.ToString());
                    }
                }
                DataSet ds = DB.ExecuteDataSet(sqlcommand);
                return ds.Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete(int roleId)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update T_SYS_Roles set ");
                strSql.Append("IsDelete=1");
                strSql.Append(" where RoleID=@RoleID");

                DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
                DB.AddInParameter(cmd, "@RoleID", DbType.Int32, roleId);
                DB.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                throw ex;
            }      
        }

        public void SetRoleAuthority(int roleId, int pageId, Dictionary<string, int> functionList)
       {
           try
           {
               StringBuilder strSql = new StringBuilder();
               strSql.Append("begin transaction ");
               strSql.Append(" delete T_SYS_RoleAuthority where roleId=@roleId and ModuleID=@pageId ");
               foreach (KeyValuePair<string, int> pair in functionList)
               {
                   strSql.Append(" insert into T_SYS_RoleAuthority(RoleID,ModuleID,FunctionID,Flag) values(@roleID,@pageId,"+pair.Key+","+pair.Value+")");
                   
               }
               strSql.Append(" IF (@@ERROR>0) ROLLBACK ELSE COMMIT ");
               DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
               DB.AddInParameter(cmd, "@roleID", DbType.Int32, roleId);
               DB.AddInParameter(cmd, "@pageId", DbType.Int32, pageId);
               DB.ExecuteNonQuery(cmd);
           }
           catch (Exception ex)
           {
               throw ex;
           }      
       }
    }
}